package com.ipbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipbd.domain.*;
import com.ipbd.dto.UserDTO;
import com.ipbd.mapper.*;
import com.ipbd.service.ActivityLogService;
import com.ipbd.service.AppUserService;
import com.ipbd.utils.UserHolder;
import com.ipbd.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86172
 * @description 针对表【activity_log(活动日志)】的数据库操作Service实现
 * @createDate 2024-03-06 20:53:48
 */
@Service
public class ActivityLogServiceImpl extends ServiceImpl<ActivityLogMapper, ActivityLog>
        implements ActivityLogService {


    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private BreathRecordMapper breathRecordMapper;
    @Autowired
    private ActivityLogMapper activityLogMapper;
    @Autowired
    private ThoughtRecordMapper thoughtRecordMapper;
    @Autowired
    private CostBenefitMapper costBenefitMapper;


    @Override
    public List<ActivityLogVo> getActivityLogList() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 获取当天的日期
        LocalDate today = LocalDate.now();
        Date dateToday = Date.valueOf(today);

        // 构建查询条件，根据当前用户id和格式化后的日期查询当天的活动日志
        QueryWrapper<ActivityLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id)
                .eq("activity_date", dateToday);
        List<ActivityLog> activityLogs = baseMapper.selectList(queryWrapper);

        // 转换为 ActivityLogVo 对象列表
        List<ActivityLogVo> activityLogVoList = new ArrayList<>();
        for (ActivityLog activityLog : activityLogs) {
            ActivityLogVo activityLogVo = new ActivityLogVo();

            // 创建 SimpleDateFormat 实例进行日期格式化
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedToday = formatter.format(dateToday);
            activityLogVo.setActivityDate(formattedToday);
            activityLogVo.setTimeQuantum(activityLog.getTimeQuantum());
            activityLogVo.setPlace(activityLog.getPlace());
            activityLogVo.setIntroduction(activityLog.getIntroduction());
            activityLogVo.setHappyDegree(activityLog.getHappyDegree());
            activityLogVoList.add(activityLogVo);
        }

        return activityLogVoList;
    }


    @Override
    public void saveActivityLog(ActivityLogForm activityLogForm) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 将ActivityLogVo转换为ActivityLog
        ActivityLog activityLog = new ActivityLog();
        activityLog.setUserId(id);
        activityLog.setActivityDate(activityLogForm.getActivityDate());
        activityLog.setTimeQuantum(activityLogForm.getTimeQuantum());
        activityLog.setPlace(activityLogForm.getPlace());
        activityLog.setIntroduction(activityLogForm.getIntroduction());
        activityLog.setHappyDegree(activityLogForm.getHappyDegree());

        // 插入数据库
        baseMapper.insert(activityLog);
    }

    @Override
    public int getWhatDay() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 获取当前用户的创建时间
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        AppUser appUser = appUserMapper.selectOne(queryWrapper);

        // 获取当前日期和创建日期
        LocalDate today = LocalDate.now();
        LocalDate createdDate = appUser.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 计算相差天数并加一
        int days = createdDate.until(today).getDays() + 1;

        return days;
    }

    @Override
    public Object getTaskData(java.util.Date day, int id) {
        // 获取用户id
        Integer userId = UserHolder.getUser().getId();
        // 获取当前用户的创建时间
        if (id == 1) {
            return getBreathRecord(day, userId);
        } else if (id == 2) {
            return getActivityLog(day, userId);
        } else if (id == 3) {
            return getThoughtRecord(day, userId);

        } else if (id == 4) {
            return getCostBenefit(day, userId);

        }
        return null;
    }

    private Object getCostBenefit(java.util.Date day, Integer userId) {
        int number = 1;
        // 构建查询条件，根据当前用户id查询记录
        QueryWrapper<CostBenefit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("record_date", day);
        List<CostBenefit> costBenefits = costBenefitMapper.selectList(queryWrapper);
        // 转换为 CostBenefitVo 对象列表
        List<CostBenefitAllVo> costBenefitVoList = new ArrayList<>();
        for (CostBenefit costBenefit : costBenefits) {
            CostBenefitAllVo costBenefitVo = new CostBenefitAllVo();
            costBenefitVo.setNumber(number);
            costBenefitVo.setObject(costBenefit.getObject());
            costBenefitVo.setDisadvantages(costBenefit.getDisadvantages());
            costBenefitVo.setAdvantages(costBenefit.getAdvantages());
            number++;
            costBenefitVoList.add(costBenefitVo);
        }
        return costBenefitVoList;
    }


    private Object getThoughtRecord(java.util.Date day, Integer userId) {
        int number = 1;
        QueryWrapper<ThoughtRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("record_date", day);
        List<ThoughtRecord> thoughtRecords = thoughtRecordMapper.selectList(queryWrapper);
        // 转换为 ThoughtRecordAllVo 对象列表
        List<ThoughtRecordAllVo> thoughtRecordAllVoList = new ArrayList<>();
        for (ThoughtRecord thoughtRecord : thoughtRecords) {
            ThoughtRecordAllVo thoughtRecordAllVo = new ThoughtRecordAllVo();
            thoughtRecordAllVo.setNumber(number);
            thoughtRecordAllVo.setEvent(thoughtRecord.getEvent());
            thoughtRecordAllVo.setThought(thoughtRecord.getThought());
            thoughtRecordAllVo.setEmotion(thoughtRecord.getEmotion());
            number++;
            thoughtRecordAllVoList.add(thoughtRecordAllVo);
        }
        return thoughtRecordAllVoList;
    }

    private Object getActivityLog(java.util.Date day, Integer userId) {
        int number = 1;
        QueryWrapper<ActivityLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("activity_date", day);
        List<ActivityLog> activityLogList = baseMapper.selectList(queryWrapper);
        List<ActivityLogAllVo> activityLogAllVoList = new ArrayList<>();
        for (ActivityLog activityLog : activityLogList) {
            ActivityLogAllVo activityLogAllVo = new ActivityLogAllVo();
            activityLogAllVo.setNumber(number);
            activityLogAllVo.setTimeQuantum(activityLog.getTimeQuantum());
            activityLogAllVo.setPlace(activityLog.getPlace());
            activityLogAllVo.setIntroduction(activityLog.getIntroduction());
            activityLogAllVo.setHappyDegree(activityLog.getHappyDegree());
            number++;
            activityLogAllVoList.add(activityLogAllVo);
        }
        return activityLogAllVoList;
    }

    private Object getBreathRecord(java.util.Date day, Integer userId) {
        int number = 1;
        QueryWrapper<BreathRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("train_time", day);
        List<BreathRecord> breathRecords = breathRecordMapper.selectList(queryWrapper);
        List<BreathRecordAllVo> breathRecordAllVoList = new ArrayList<>();
        for (BreathRecord breathRecord : breathRecords) {
            BreathRecordAllVo breathRecordAllVo = new BreathRecordAllVo();
            breathRecordAllVo.setNumber(number);
            breathRecordAllVo.setBeginAnxiety(breathRecord.getBeginAnxiety());
            breathRecordAllVo.setEndAnxiety(breathRecord.getEndAnxiety());
            breathRecordAllVo.setBeginUnhappy(breathRecord.getBeginUnhappy());
            breathRecordAllVo.setEndUnhappy(breathRecord.getEndUnhappy());
            breathRecordAllVo.setText(breathRecord.getText());
            number++;
            breathRecordAllVoList.add(breathRecordAllVo);
        }
        return breathRecordAllVoList;
    }


}




